package com.software.backend.searchmovie.utils;


import com.software.backend.searchmovie.entity.Video;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SearchMovieUtil {

    /**
     * 获取文件夹中文件的全部文件名
     * @param dirPath 要搜索的文件夹路径
     * @return List<String>该文件夹下的所有文件名
     */
    public static List<String> scanAllFileFromDir(String dirPath) throws Exception {
        File fileDir = new File(dirPath);
        if (!fileDir.isDirectory()) {
            throw new Exception("文件夹目录错误");
        }
        File[] files = fileDir.listFiles();
        List<String> fileNames = new ArrayList<>();
        for (File file : files) {
            if (file.isFile()) {
                fileNames.add(file.getName());
            }
        }
        return fileNames;
    }


    /**
     * 搜索指定文件中的关键字
     *
     * @param filePath  要搜索的文件路径
     * @param searchStr 要搜索的关键字
     * @return 返回的 map<行数, 该行内容>
     */
//    public static List<Movie.Part> scanFile(String filePath, String searchStr) {
//        List<Movie.Part> parts = new ArrayList<>();
//        try {
//            FileInputStream file = new FileInputStream(filePath);
//            InputStreamReader in = new InputStreamReader(file, "UTF-8"); //字节流转化为字符流，以GBK读取防止中文乱码
//            BufferedReader buf = new BufferedReader(in); //加入到缓存区
//            String str = "";
//            while ((str = buf.readLine()) != null) { //按行读取，到达最后一行返回null
//                if (str.contains(searchStr)) {
//                    Movie.Part part = formatDialog(str);
//                    parts.add(part);
//                }
//            }
//            buf.close();
//            file.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return parts;
//    }

    public static List<Video> scanFile(String filePath, String searchStr) {
        List<Video> videoList = new ArrayList<>();
        try {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            InputStreamReader in  = new InputStreamReader(fileInputStream,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(in);
            String str = "";
            while ((str = bufferedReader.readLine()) !=null){
                if (str.contains(searchStr)){
                    Video video = formatDialog(str);
                    videoList.add(video);
                }
            }
            bufferedReader.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return videoList;
    }

    private static Video formatDialog(String str) {
        Video video = new Video();
        String[] strings  = str.split(",");
        video.setStartTime(strings[1].split("\\.")[0]);
        String startTime = strings[1];
        String endTime = strings[2];
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date start = null;
        Date end = null;
        try {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } finally {
        }
        long startTimeMills = start.getTime() + 28800000L;
//     System.out.println(startTimeMills);
        long endTimeMills = end.getTime() + 28800000L;
//        Date date = new Date(endTimeMills - startTimeMills - 28800000L);
//        String time = sdf.format(date);
        // System.out.println(time);
        long duration = endTimeMills - startTimeMills;
        if (duration <=1000 ){
            duration =2000;
        }else{
            duration+=1000;
        }
        video.setDuration((int) duration);
        String[] subtitle = strings[9].split("\\\\");
        video.setOriginalContent(subtitle[0]);
        String[] englishSubtitle = str.split("}");
        video.setTranslatedContent(englishSubtitle[1]);
        video.setLanguageType("English");
        video.setEmotion("happy");
//        video.setId(1);
        return video;
    }


    /**
     * 搜索指定文件中的关键字
     *
     * @param str ass文件中一行台词
     * @return 返回的 map<行数, 该行内容>
     */
//    public static Movie.Part formatDialog(String str) {
//        Movie.Part part = new Movie.Part();
//        String[] strings = str.split(",");
//        part.setStartTime(strings[1]);
//        part.setEndTime(strings[2]);
//        String[] subtitle = strings[9].split("\\\\");
//        part.setChineseSubtitle(subtitle[0]);
//        String[] englishSubtitle = subtitle[3].split("}");
//        part.setForeignSubtitle(englishSubtitle[1]);
//        return part;
//    }


    }

